package com.qk.management.controller;

import com.qk.common.util.PageResult;
import com.qk.common.util.Result;
import com.qk.domain.user.UserDo;
import com.qk.dto.user.UserDto;
import com.qk.entity.User;
import com.qk.management.anno.LogOperation;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户管理
     * 1、搭架子
     * 2、补参数
     * 3、写方法
     */
    @GetMapping
    public Result getUser(UserDto userDto) {
        PageResult<UserDo> userpageResult = userService.getUser(userDto);
        log.info("分页查询用户：{}", userpageResult);
        return Result.success(userpageResult);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Integer userid) {
        log.info("根据ID查询用户：{}", userid);
        User user = userService.selectById(userid);
        return Result.success(user);
    }
    @GetMapping("/list")
    public Result selectList() {
        log.info("查询所有用户");
        List<User> userList = userService.selectList();
        return Result.success(userList);
    }
    @GetMapping("/role/{roleLabel}")
    public Result selectByRole(@PathVariable("roleLabel") String roleLabel) {
        log.info("根据角色查询用户：{}", roleLabel);
        List<User> userList = userService.selectByRole(roleLabel);
        return Result.success(userList);
    }
    @GetMapping("/dept/{deptId}")
    public Result selectByDept(@PathVariable("deptId") Integer deptId) {
        log.info("根据部门查询用户：{}", deptId);
        List<User> userList = userService.selectByDept(deptId);
        return Result.success(userList);
    }

    @PutMapping
    @LogOperation
    public Result updateUser(@RequestBody User user) {
        log.info("修改用户：{}", user);
        userService.updateUser(user);
        return Result.success();
    }
    @PostMapping
    @LogOperation
    public Result addUser(@RequestBody User user) {
        log.info("添加用户：{}", user);
        userService.addUser(user);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    @LogOperation
    public Result deleteUser(@PathVariable List<Integer> ids) {
        log.info("删除用户：{}", ids);
        userService.deleteUser(ids);
        return Result.success();
    }
}
